﻿// JScript File
var svcUrl = '../services/EditTemplate.aspx';
var ddlTemplate = null;
var txtTemplate = null;
var btnSave = null;
var btnNew = null;
var newtemplateform=null;
var btnCreate = null;
var txtTemplateName=null;
var ifPreview = null;
var btnUpdatePreview = null;

window.onload = function() 
{
  init();
};

function init()
{
    ddlTemplate = $('ddlTemplate');
    txtTemplate = $('txtTemplate');
    btnSave = $('btnSave');
    btnNew = $('btnNew');
    newtemplateform = $('ntf');
    btnCreate = $('btnCreate');
    txtTemplateName = $('tname');
    ifPreview = $('ifPreview');
    btnUpdatePreview = $('btnUpdatePreview');
    
    Event.observe(ddlTemplate, 'change', gt);
    Event.observe(btnSave, 'click', save);
    Event.observe(txtTemplate, 'keydown', function() { btnSave.removeAttribute('disabled'); btnSave.setAttribute('value', 'Save Changes'); });
    Event.observe(btnNew, 'click', newTemplate);
    Event.observe(btnCreate, 'click', createTemplate);
    Event.observe(btnUpdatePreview, 'click', refreshPreview);

    databind();  
}

function databind()
{
    new Ajax.Request(svcUrl, {
        onSuccess: onGetTemplatesSuccess,
        method:'get',
        parameters:'method=GetTemplates'
    });
}

function onGetTemplatesSuccess(transport)
{
    var templates = transport.responseText.split('#').toArray();
    var c = templates.length;
    ddlTemplate.innerHTML='';
    ddlTemplate.appendChild($option('','Choose a template...'));
    
    for(var i=0; i<c; i++)
    {
        if (templates[i].length == 0)
            continue;
            
        ddlTemplate.appendChild($option(templates[i], templates[i]));
    }
    
    btnUpdatePreview.removeAttribute('disabled');
}

function $option(value,text)
{
    var o = document.createElement('option');
    o.setAttribute('value', value);
    o.innerHTML = text;
    return o;
}

// get template
function gt()
{
    txtTemplate.innerHTML = "Loading template...";
    if (ddlTemplate.selectedIndex == 0)
        return;
    var template = ddlTemplate.options[ddlTemplate.selectedIndex].value;

    new Ajax.Request(svcUrl, {
        onSuccess: onGetTemplateSuccess,
        method:'get',
        parameters:'method=GetTemplate&tname='+template
    });
}

function onGetTemplateSuccess(transport)
{
    txtTemplate.innerHTML = transport.responseText;
    updateTempPreview();
    btnUpdatePreview.removeAttribute('disabled');
}

function saveTemp()
{

}

function refreshPreview()
{
    var templatePath = ddlTemplate.options[ddlTemplate.selectedIndex].value;
    var template = txtTemplate.value;
    template = encodeURIComponent(template);
    //template = encodeURI(template);
    var params = 'method=SaveTemplate&tname='+templatePath+'&t='+template+'&temp=true';
    new Ajax.Request(svcUrl, {
        onSuccess: updateTempPreview,
        method:'post',parameters:params
    });
}

function updatePreview()
{
    var templatePath = ddlTemplate.options[ddlTemplate.selectedIndex].value;
    ifPreview.src = 'TemplatePreview.aspx?tp='+templatePath;
    
}
function updateTempPreview()
{
    var templatePath = ddlTemplate.options[ddlTemplate.selectedIndex].value;
    ifPreview.src = 'TemplatePreview.aspx?tp='+templatePath+'&temp=true';
    
}

function save()
{
    btnSave.setAttribute('disabled', 'disabled');
    btnSave.setAttribute('value', 'Saving...');
    
    var templatePath = ddlTemplate.options[ddlTemplate.selectedIndex].value;
    var template = encodeURIComponent(txtTemplate.value);
    var params = 'method=SaveTemplate&tname='+templatePath+'&t='+template;
    new Ajax.Request(svcUrl, {
        onSuccess: onSaveTemplateSuccess,
        method:'post',parameters:params
    });
    
    updatePreview();
    
}

function newTemplate()
{
    newtemplateform.style.display='inline';
    txtTemplateName.value = '';
}

function createTemplate()
{
    new Ajax.Request(svcUrl, {
        onSuccess: onCreateTemplateSuccess,
        method:'get',
        parameters:'method=CreateTemplate&tname='+txtTemplateName.value
    });
}

function onCreateTemplateSuccess(transport)
{
    newtemplateform.style.display='none';
    //databind();
    var templateData = transport.responseText.split('#');
    ddlTemplate.appendChild($option(templateData[0], templateData[0]));
    ddlTemplate.selectedIndex=ddlTemplate.options.length-1;
    gt();
}

function onSaveTemplateSuccess()
{
    btnSave.setAttribute('value', 'Changes Saved');
}